//
//  ChildController04.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/13.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChildController04.h"
#import "ChildViewCell04.h"
#import "NoOrderView.h"

@interface ChildController04 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *ChildTable04;
@property (strong, nonatomic) NSMutableArray * OrderArr04;
@property (assign, nonatomic) NSUInteger page;
@property (strong, nonatomic) MBProgressHUD * hud;
@end

@implementation ChildController04

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.OrderArr04.count == 0) {
        [self.ChildTable04.mj_header beginRefreshing];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.ChildTable04.delegate = self;
    self.ChildTable04.dataSource = self;
    self.ChildTable04.rowHeight = 120;
    self.ChildTable04.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.ChildTable04 registerNib:[UINib nibWithNibName:@"ChildViewCell04" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.ChildTable04.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.OrderArr04 removeAllObjects];
        [self UploadDataOfChildController04];
    }];
    
    self.ChildTable04.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        [self UploadDataOfChildController04];
    }];
    
}

- (void)UploadDataOfChildController04{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [dic setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"page"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"received" andAction:@"Mypos" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求数据结果 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                ChildModel04 * model = [ChildModel04 mj_objectWithKeyValues:dic];
                [self.OrderArr04 addObject:model];
                
            }
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
        }
        [self.ChildTable04.mj_header endRefreshing];
        [self.ChildTable04.mj_footer endRefreshing];
        [self.ChildTable04 reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [self.ChildTable04.mj_header endRefreshing];
        [self.ChildTable04.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.OrderArr04.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChildViewCell04 * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ChildViewCell04 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell CreateChildViewCell04];
    
    ChildModel04 * model = self.OrderArr04[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.OrderArr04.count == 0) {
        return self.ChildTable04.bounds.size.height;
    }else{
        return 0.1;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NoOrderView * headV = [[NSBundle mainBundle] loadNibNamed:@"NoOrderView" owner:self options:nil][0];
    if (self.OrderArr04.count == 0) {
        headV.BackView.hidden = NO;
    }else{
        headV.BackView.hidden = YES;
    }
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * Foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 0.1)];
    return Foot;
}


- (NSMutableArray *)OrderArr04 {
    if (!_OrderArr04) {
        _OrderArr04 = [NSMutableArray array];
    }
    return _OrderArr04;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
